package com.seele.tigerwallet.ui.orm;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;


import com.j256.ormlite.android.DatabaseTableConfigUtil;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTableConfig;
import com.j256.ormlite.table.TableUtils;
import com.seele.tigerwallet.model.AssetsModel;
import com.seele.tigerwallet.model.ContactsModel;
import com.seele.tigerwallet.model.MarketModel;
import com.seele.tigerwallet.model.MessageModel;
import com.seele.tigerwallet.model.TokenInfoModel;
import com.seele.tigerwallet.model.TsModel;
import com.seele.tigerwallet.model.TsRecord;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 *      author : xiaohu
 *      time   : 2018/1/23
 *      des    :    orm数据库帮助者
 *  <pre/>
 */

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static DatabaseHelper mHelper;
    private static Context context;
    private Map<String, Dao> daos = new HashMap<>();

    public DatabaseHelper(Context context) {
        //把数据库放到sd卡上面
//        super(context, PathUtils.getFilePath(PathUtils.FNG_YU,PathUtils.DB_NAME), null, 1);

        super(context, "tiger_wallet.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            //创建表
            TableUtils.createTable(connectionSource, TokenInfoModel.class);
            TableUtils.createTable(connectionSource, MarketModel.class);
            TableUtils.createTable(connectionSource, AssetsModel.class);
            TableUtils.createTable(connectionSource, ContactsModel.class);
            TableUtils.createTable(connectionSource, TsModel.class);
            TableUtils.createTable(connectionSource, MessageModel.class);
            TableUtils.createTable(connectionSource, TsRecord.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {

    }


    public static synchronized DatabaseHelper getInstance(){
        if(mHelper == null){
            synchronized (DatabaseHelper.class){
                if(mHelper == null){
                    mHelper = new DatabaseHelper(context);
                }
            }
        }
        return mHelper;
    }

    public static void setApplication(Context mApplication) {
        DatabaseHelper.context = mApplication;
    }

    @Override
    public void close() {
        super.close();
        for (String key : daos.keySet()){
            Dao d = daos.get(key);
            d = null;
        }
    }





    public synchronized Dao getDao(Class clazz) throws SQLException {
        if (null == clazz) {
            return null;
        }
        Dao dao = null;

        DatabaseTableConfig config = DatabaseTableConfigUtil.fromClass(getConnectionSource(), clazz);
        String daoKey = clazz.getSimpleName() + "_" + config.getTableName();


        if (daos.containsKey(daoKey)) {
            dao = daos.get(daoKey);
        }
        if (dao == null) {
            DaoManager.createDao(getConnectionSource(), config);

            dao = super.getDao(clazz);
            daos.put(daoKey, dao);
        }
        return dao;
    }


//    @SuppressWarnings("unchecked")
//    public synchronized Dao getDao(Class clazz) throws SQLException {
//        Dao dao;
//        String simpleName = clazz.getSimpleName();
//        if(daos.containsKey(simpleName)){
//            dao = daos.get(simpleName);
//        }else{
//            dao = super.getDao(clazz);
//            daos.put(simpleName,dao);
//        }
//        return dao;
//    }



    public boolean openDatabase(){
        return this.isOpen();
    }

    public void closeDatabase(){
        this.close();
    }
}
